package com.neusoft.databus.policy.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.databus.policy.dto.EventDTO;
import com.neusoft.databus.policy.model.Event;
import com.neusoft.databus.policy.repository.EventRepository;
import com.neusoft.databus.policy.vo.EventVO;

/**
 * 事件Converter
 *
 * @author naxiang
 */
@Component
public class EventConverter extends AbstractConverter<Event, EventDTO> {
    @Autowired
    private EventRepository eventRepository;
    @Autowired
    private PolicyConverter policyConverter;

    @Override
    public Event toModel(final EventDTO dto) {
        Event model;
        if (dto.isNew()) {
            model = new Event();
        } else {
            model = this.eventRepository.getOne(dto.getId());
        }
        model.setStatus(dto.getStatus());
        model.setResourceCode(dto.getResourceCode());
        if (null != dto.getPolicy()) {
            model.setPolicy(this.policyConverter.toModel(dto.getPolicy()));
        }
        model.setMetricCode(dto.getMetricCode());
        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());
        return model;
    }

    @Override
    public EventDTO toDTO(final Event model, final boolean forListView) {
        final EventDTO eventDTO = new EventDTO();
        eventDTO.setId(model.getId());
        eventDTO.setCode(model.getCode());
        eventDTO.setStatus(model.getStatus());
        eventDTO.setFixTime(model.getFixTime());
        if (null != model.getEventAttribute()) {
            eventDTO.setCount(model.getEventAttribute().getCount() == null ? 1L : model.getEventAttribute().getCount());
            eventDTO.setContent(model.getEventAttribute().getContent());
            eventDTO.setLastTime(model.getEventAttribute().getLastTime());
        }
        eventDTO.setResourceCode(model.getResourceCode());
        if (null != model.getPolicy()) {
            eventDTO.setPolicy(this.policyConverter.toDTO(model.getPolicy()));
        }
        eventDTO.setMetricCode(model.getMetricCode());
        eventDTO.setCreateTime(model.getCreateTime());
        eventDTO.setUpdateTime(model.getUpdateTime());
        return eventDTO;
    }

    public EventVO toVO(final Event model) {
        final EventVO eventVO = new EventVO();
        eventVO.setId(model.getId());
        eventVO.setCode(model.getCode());
        eventVO.setStatus(model.getStatus());
        eventVO.setFixTime(model.getFixTime());
        if (null != model.getEventAttribute()) {
            eventVO.setCount(model.getEventAttribute().getCount() == null ? 1L : model.getEventAttribute().getCount());
            eventVO.setContent(model.getEventAttribute().getContent());
            eventVO.setLastTime(model.getEventAttribute().getLastTime());
        }
        eventVO.setResourceCode(model.getResourceCode());
        eventVO.setPolicyCode(model.getPolicy().getCode());
        eventVO.setType(model.getPolicy().getPolicyType());
        eventVO.setMetricCode(model.getMetricCode());
        return eventVO;

    }

}
